package com.sxfq.loansupermarket.backsystem.modular.weixin.controller;

import com.sxfq.loansupermarket.backsystem.GunsApplication;
import com.sxfq.loansupermarket.backsystem.common.constant.factory.ConstantFactory;
import com.sxfq.loansupermarket.backsystem.common.controller.BaseController;
import com.sxfq.loansupermarket.backsystem.common.exception.BizExceptionEnum;
import com.sxfq.loansupermarket.backsystem.common.exception.BussinessException;
import com.sxfq.loansupermarket.backsystem.common.persistence.dao.UserMapper;
import javax.servlet.http.HttpServletRequest;
import com.sxfq.loansupermarket.backsystem.common.persistence.model.User;
import com.sxfq.loansupermarket.backsystem.core.log.LogManager;
import com.sxfq.loansupermarket.backsystem.core.log.LogObjectHolder;
import com.sxfq.loansupermarket.backsystem.core.log.factory.LogTaskFactory;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroUser;
import com.sxfq.loansupermarket.backsystem.core.support.HttpKit;
import com.sxfq.loansupermarket.backsystem.core.util.ToolUtil;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.basicData.service.IGetListService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * @author py
 */
@Controller
@RequestMapping("/appMy")
public class AppMyController extends BaseController {
    @Resource
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SqlMapper sqlMapper;

    @Autowired
    IGetListService getListService;

    private String PREFIX = "/weixin/app/my";


    protected final static Logger logger = LoggerFactory.getLogger(GunsApplication.class);

    /**
     * 跳转到我的页面
     */
    @RequestMapping(value = "/my", method = RequestMethod.GET)
    public String my(Model model, HttpServletResponse response) {
        Integer userId = ShiroKit.getUser().getId();
        if (ToolUtil.isEmpty(userId)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        User user = this.userMapper.selectById(userId);
        if (user.getAvatar() == null || user.getAvatar() == "") {
            user.setAvatar("https://pynnn.oss-cn-hangzhou.aliyuncs.com/yiyalogo.png");
        }
        model.addAttribute(user);
        model.addAttribute("roleName", ConstantFactory.me().getRoleName(user.getRoleid()));
        model.addAttribute("deptName", ConstantFactory.me().getDeptName(user.getDeptid()));
        LogObjectHolder.me().set(user);
        model.addAttribute("account", user.getAccount());
        model.addAttribute("avatar", user.getAvatar());
        return PREFIX + "/my.html";
    }

    /**
     * 跳转到个人中心
     */
    @RequestMapping(value = "/personalCentres", method = RequestMethod.GET)
    public String personalCentres(Model model, HttpServletResponse response) {
        model.addAttribute("account", ShiroKit.getUser().account);
        return PREFIX + "/personalCentres.html";
    }

    /**
     * 跳转到个人资料
     */
    @RequestMapping("/updatePersonal")
    public String to_updatePersonal(Model model) {
        Integer id = ShiroKit.getUser().getId();
        User user = userMapper.selectById(id);
        model.addAttribute("roleName", redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"RoleName",0));
        model.addAttribute("user", user);
        return PREFIX + "/updatePersonal.html";
    }

    /**
     * 跳转到修改密码
     */
    @RequestMapping("/toUpdatePassWord")
    public String to_updatePassWord(Model model) {
        Integer id = ShiroKit.getUser().getId();
        User user = userMapper.selectById(id);
        model.addAttribute("orPassWord", user.getPassword());
        return PREFIX + "/updatePassWord.html";
    }

    /**
     * 判定科室登记
     */
    @RequestMapping(value = "/editSubmit", method = RequestMethod.POST)
    @ResponseBody
    public int editSubmit(String sex, String email, String phone, Model model) {
        String upsql = "update user set sex='"+sex+"',email='"+email+"',phone='"+phone+"' where id='"+ShiroKit.getUser().getId()+"'";
        return sqlMapper.update(upsql);
    }

    @RequestMapping(value = "/updatePassWord", method = RequestMethod.POST)
    @ResponseBody
    public String updatePassWord() {
        Integer id = ShiroKit.getUser().getId();
        User user = userMapper.selectById(id);
        String newPassWord = super.getPara("newPassWord").trim();
        String passWord = super.getPara("passWord").trim();
        String passWordMd5 = ShiroKit.md5(passWord, user.getSalt());
        if (passWordMd5.equals(user.getPassword())) {
            String newPassWordMd5 = ShiroKit.md5(newPassWord, user.getSalt());
            user.setPassword(newPassWordMd5);
            userMapper.updateById(user);
            return "0";
        } else {
            return "1";
        }
    }

    /**
     * 退出登录
     */
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public String logOut(HttpServletRequest request, HttpServletResponse response, Object handler) {
        ShiroUser ss = ShiroKit.getUser();
        LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
        ShiroKit.getSubject().logout();
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        return REDIRECT + "/weixin/index";
    }

}